package com.hxzy.booking.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hxzy.booking.mapper.BookingMapper;
import com.hxzy.booking.mapper.SignInMapper;
import com.hxzy.booking.pojo.Booking;
import com.hxzy.booking.pojo.SignIn;
import com.hxzy.booking.service.BookingService;
import com.hxzy.course.mapper.ArrangeCourseMapper;
import com.hxzy.course.mapper.CourseMapper;
import com.hxzy.course.mapper.StaffMapper;
import com.hxzy.course.mapper.TimeMapper;
import com.hxzy.course.pojo.ArrangeCourse;
import com.hxzy.course.pojo.Course;
import com.hxzy.course.pojo.Staff;
import com.hxzy.course.pojo.Time;
import com.hxzy.member.dao.MemberDao;
import com.hxzy.member.pojo.Member;
import com.hxzy.region.dao.RegionMapper;
import com.hxzy.region.pojo.Region;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.print.Book;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@Service
public class BookingServiceImpl implements BookingService {

    @Autowired
    private BookingMapper bookingMapper;

    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private StaffMapper staffMapper;

    @Autowired
    private RegionMapper regionMapper;

    @Autowired
    private SignInMapper signInMapper;

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private ArrangeCourseMapper arrangeCourseMapper;

    @Autowired
    private TimeMapper timeMapper;

    @Override
    public Page<Booking> findAll(Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<Booking> empLambdaQueryWrapper= Wrappers.lambdaQuery();
        empLambdaQueryWrapper.select();

        if(pageNum == null){
            pageNum = 1;
        }
        if(pageSize == null){
            pageSize = 10;
        }

        Page<Booking> empPage=new Page<>(pageNum,pageSize);
        Page<Booking> empIPage=bookingMapper.selectPage(empPage, empLambdaQueryWrapper);
        return empIPage;
    }

    @Override
    public Page<Booking> findAllNotSign(Integer pageNum, Integer pageSize) {
        QueryWrapper<Booking> empLambdaQueryWrapper= new QueryWrapper<>();
        empLambdaQueryWrapper.eq("b_flag",0);

        if(pageNum == null){
            pageNum = 1;
        }
        if(pageSize == null){
            pageSize = 10;
        }

        Page<Booking> empPage=new Page<>(pageNum,pageSize);
        Page<Booking> empIPage=bookingMapper.selectPage(empPage, empLambdaQueryWrapper);
        return empIPage;
    }

    @Override
    public Page<SignIn> findAllSignIn(Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<SignIn> empLambdaQueryWrapper= Wrappers.lambdaQuery();
        empLambdaQueryWrapper.select();

        Page<SignIn> empPage=new Page<>(pageNum,pageSize);
        Page<SignIn> empIPage=signInMapper.selectPage(empPage, empLambdaQueryWrapper);
        return empIPage;
    }

    @Override
    public List<Region> findAllRegion() {
        return regionMapper.selectList(null);
    }

    @Override
    public List<Staff> findAllStaff() {
        List<Staff> allStaff = staffMapper.findAllStaff();
        return allStaff;
    }

    @Override
    public List<Course> findAllCourse() {
        return courseMapper.selectList(null);
    }

    @Override
    public Page<Booking> likeNameCourse(String regionName, String coachName, String courseName, Integer flag,
                                        String bookingTime,String schoolTime, String card, String tel,
                                        String name,Integer pageNum, Integer pageSize) {
        QueryWrapper<Booking> queryWrapper=new QueryWrapper<>();

        if(pageNum == null){
            pageNum = 1;
        }
        if(pageSize == null){
            pageSize = 10;
        }

        if(((coachName != null && !"".equals(coachName)) || (courseName != null && !"".equals(courseName))
                || (flag != null && !"".equals(flag)) || (bookingTime != null && !"".equals(bookingTime) )
                || (schoolTime != null && !"".equals(schoolTime)))
                || ((card != null && !"".equals(card)) && (tel != null && !"".equals(tel)) && (name != null && !"".equals(name)))){
            System.out.println("多个条件查询执行了");
            queryWrapper.like("stadium",regionName);
            queryWrapper.and(qw -> {
                if (coachName != null && !"".equals(coachName)){
                    qw.like("coach_name",coachName);
                }
                if (courseName != null && !"".equals(courseName)){
                    qw.like("b_course_name",courseName);
                }
                if (flag != null && !"".equals(flag)){
                    qw.like("b_flag",flag);
                }
                if(bookingTime != null && !"".equals(bookingTime)){
                    qw.like("b_bookingtime",bookingTime);
                }
                if(schoolTime != null && !"".equals(schoolTime)){
                    qw.like("b_schooltime",schoolTime);
                }
                if(card != null && !"".equals(card)){
                    qw.like("member_card",card);
                }
                if(tel != null && !"".equals(tel)){
                    if(card == null && !"".equals(card)){
                        qw.like("b_tel",tel);
                    }else{
                        qw.or();
                        qw.like("b_tel",tel);
                    }
                }
                if (name != null && !"".equals(name)){
                    if ((card == null && !"".equals(card)) || (tel == null && !"".equals(tel))){
                        qw.like("member_name",name);
                    }else {
                        qw.or();
                        qw.like("member_name",name);
                    }
                }
            });
            Page<Booking> empPage=new Page<>(pageNum,pageSize);
            Page<Booking> empIPage=bookingMapper.selectPage(empPage, queryWrapper);
            return empIPage;
        }else {
            System.out.println("单独查询场馆执行了");
            queryWrapper.like("stadium",regionName);
            Page<Booking> empPage=new Page<>(pageNum,pageSize);
            Page<Booking> empIPage=bookingMapper.selectPage(empPage, queryWrapper);
            return empIPage;
        }

    }

    @Override
    public Page<SignIn> searchSign(String sTime, String name, String card,Integer pageNum, Integer pageSize) {
        QueryWrapper<SignIn> wrapper=new QueryWrapper<>();
        if(pageNum == null){
            pageNum = 1;
        }
        if(pageSize == null){
            pageSize = 10;
        }
        if ( (card != null && !"".equals(card))  && (name != null && !"".equals(name))){
            System.out.println("多个条件查询执行了");
            wrapper.like("s_time",sTime);
            wrapper.and(signInQueryWrapper -> {
                if(card != null && !"".equals(card)){
                    signInQueryWrapper.like("member_card",card);
                }
                if(name != null && !"".equals(name)){
                    if(card == null && !"".equals(card)){
                        signInQueryWrapper.like("member_name",name);
                    }else{
                        signInQueryWrapper.or();
                        signInQueryWrapper.like("member_name",name);
                    }
                }
            });
            Page<SignIn> empPage=new Page<>(pageNum,pageSize);
            Page<SignIn> empIPage=signInMapper.selectPage(empPage, wrapper);
            return empIPage;
        }else {
            System.out.println("单独查询场馆执行了");
            wrapper.like("s_time",sTime);
            Page<SignIn> empPage=new Page<>(pageNum,pageSize);
            Page<SignIn> empIPage=signInMapper.selectPage(empPage, wrapper);
            return empIPage;
        }
    }

    @Override
    public List<Staff> findAllSell() {
        return staffMapper.findAllSell();
    }

    @Override
    public boolean addBooking(String memberCard, String bGender, String bTel, String sellName,
                              String bcourseName, Integer bFlag, String bRemark, Integer arrangeId) {
        System.out.println("添加："+memberCard+bGender+bTel+sellName+bFlag+bRemark+arrangeId);
        //通过会员号查询会员名和会员编号
        QueryWrapper<Member> wrapper=new QueryWrapper();
        wrapper.eq("m_card",memberCard);
        List<Member> members = memberDao.selectList(wrapper);
        Integer mId=null;
        String mName=null;
        for (Member m : members) {
            mId=m.getMId();
            mName=m.getMName();
        }

        //通过销售名查询销售编号
        QueryWrapper<Staff> wrapper1=new QueryWrapper<>();
        wrapper1.eq("staff_name",sellName);
        List<Staff> staff = staffMapper.selectList(wrapper1);
        Integer sId=null;
        for (Staff s : staff) {
            sId=s.getStaffId();
        }

        //通过课程名称查询课程编号
        QueryWrapper<Course> wrapper2=new QueryWrapper<>();
        wrapper2.eq("co_name",bcourseName);
        List<Course> courses = courseMapper.selectList(wrapper2);
        Integer cId=null;
        for (Course c : courses) {
            cId=c.getCoId();
        }

        //通过课表编号查询所属课表信息
        QueryWrapper<ArrangeCourse> wrapper3=new QueryWrapper<>();
        wrapper3.eq("arrange_id",arrangeId);
        List<ArrangeCourse> arrangeCourses = arrangeCourseMapper.selectList(wrapper3);
        Integer coachId=null;
        Integer regionId=null;
        String bSchoolTime=null;
        Integer tId=null;
        for (ArrangeCourse a : arrangeCourses) {
            coachId=a.getStaffId();
            regionId = a.getTRegionId();
            bSchoolTime=a.getDate();
            tId=a.getTiId();
        }

        //通过教练编号查询教练姓名
        QueryWrapper<Staff> wrapper4=new QueryWrapper<>();
        wrapper4.eq("staff_id", coachId);
        List<Staff> staff2 = staffMapper.selectList(wrapper4);
        String coachName=null;
        for (Staff staff1  :staff2) {
            coachName=staff1.getStaffName();
        }

        //通过场馆编号查询场馆名称
        QueryWrapper<Region> wrapper6=new QueryWrapper<>();
        wrapper6.eq("t_region_id",regionId);
        List<Region> regions = regionMapper.selectList(wrapper6);
        String stadium=null;
        for (Region r : regions) {
            stadium=r.getTRegionName();
        }

        //通过课程时间编号查询课程时间
        QueryWrapper<Time> wrapper5=new QueryWrapper<>();
        wrapper5.eq("ti_id",tId);
        List<Time> times = timeMapper.selectList(wrapper5);
        String tiTime=null;
        for (Time t : times) {
            tiTime=t.getTiTime();
        }

        Booking booking=new Booking();
        booking.setMemberId(mId);
        booking.setMemberName(mName);
        booking.setMemberCard(memberCard);
        booking.setBGender(bGender);
        booking.setBTel(bTel);
        booking.setSellId(sId);
        booking.setSellName(sellName);

        booking.setBCourseId(cId);
        booking.setBCourseName(bcourseName);

        booking.setCoachId(coachId);
        booking.setCoachName(coachName);

        booking.setStadium(stadium);

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = simpleDateFormat.format(new Date());
        booking.setBBookingtime(time);

        booking.setBSchooltime(bSchoolTime);
        booking.setBCoursetime(tiTime);

        booking.setBFlag(0);
        booking.setBRemark(bRemark);
        booking.setArrangeId(arrangeId);
        int insert = bookingMapper.insert(booking);
        return insert>0;
    }

    @Override
    public boolean deleteBooking(int bid) {
        System.out.println("删除id："+bid);
        int i = bookingMapper.deleteById(bid);
        return i>0;
    }

    @Override
    public boolean updateBooking(Booking booking) {
        booking.setBId(booking.getBId());
        booking.setMemberCard(booking.getMemberCard());
        booking.setBGender(booking.getBGender());
        booking.setBTel(booking.getBTel());
        booking.setStadium(booking.getStadium());
        booking.setSellName(booking.getSellName());
        booking.setCoachName(booking.getCoachName());
        booking.setBCourseName(booking.getBCourseName());
        booking.setBBookingtime(booking.getBBookingtime());
        booking.setBSchooltime(booking.getBSchooltime());
        booking.setBFlag(booking.getBFlag());
        booking.setBRemark(booking.getBRemark());
        int i = bookingMapper.updateById(booking);
        return i>0;
    }

    @Override
    public boolean signInBooking(int bid,String coName) {

        QueryWrapper<Course> wrapper=new QueryWrapper<>();
        wrapper.eq("co_name",coName);
        List<Course> courses = courseMapper.selectList(wrapper);
        String cost=null;
        for (Course c : courses) {
            cost=c.getCoPrice();
        }

        Booking booking=new Booking();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = simpleDateFormat.format(new Date());
        booking.setBId(bid);
        booking.setBSigntime(time);
        booking.setBWay("场馆签到");
        booking.setBCost(cost);
        booking.setBFlag(1);
        int i = bookingMapper.updateById(booking);
        return i>0;
    }

    @Override
    public List<Booking> export() {
        return bookingMapper.selectList(null);
    }

}
